package ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.ToolTipManager;
import javax.swing.text.AbstractDocument;

/** Gui for deposit an amount 
 * @author Matthias 
 * @author Niels Christian
 *
 */
public class AccDeposit extends JPanel {
	
	private final JLabel lbl_Header;
	private final JLabel lbl_Intro;
	private final JButton btn_Back;
	private final JButton btn_Deposit;
	private final JTextField textField_Deposit;
	private final JTextField textField_CurrentBalance;
	private final JLabel lbl_CurrentBalance;
	private final JLabel logo_ATM;
	
	/**
	 * double object, for deposit amount
	 */
	private double depositAmount;
	
	/** Constructor of AccDeposit
	 * Contains the information/blueprint on labels, buttons and text fields.
	 * Sets also the window Dimensions
	 */
	public AccDeposit() {
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);
		
		lbl_Header = new JLabel("Account Deposit");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(221, 31, 207, 34);
		add(lbl_Header);
		
		lbl_Intro = new JLabel("<html><p>Please enter the amount <br> you want to deposit: </p></html>");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(221, 182, 211, 47);
		add(lbl_Intro);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(100, 354, 130, 50);
		add(btn_Back);
		
		btn_Deposit = new JButton("Deposit");
		btn_Deposit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Deposit.setBounds(411, 354, 130, 50);
		add(btn_Deposit);
		
		textField_Deposit = new JTextField();
		textField_Deposit.addMouseListener(mouseListener);
		textField_Deposit.setToolTipText("Maximum amount per deposit: 99999");
		((AbstractDocument)textField_Deposit.getDocument()).setDocumentFilter(new LimitOfChar(5));
		textField_Deposit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Deposit.setBounds(221, 230, 190, 35);
		add(textField_Deposit);
		textField_Deposit.setColumns(10);
		
		textField_CurrentBalance = new JTextField();
		textField_CurrentBalance.setHorizontalAlignment(SwingConstants.CENTER);
		textField_CurrentBalance.setEditable(false);
		textField_CurrentBalance.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		textField_CurrentBalance.setBounds(221, 136, 190, 35);
		add(textField_CurrentBalance);
		textField_CurrentBalance.setColumns(10);
		
		lbl_CurrentBalance = new JLabel("Current Balance:");
		lbl_CurrentBalance.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_CurrentBalance.setBounds(220, 109, 130, 21);
		add(lbl_CurrentBalance);
		
		logo_ATM = new JLabel("");
		logo_ATM.setIcon(new ImageIcon(AccDeposit.class.getResource("/pic/LOGO Mini.png")));
		logo_ATM.setBounds(15, 15, 50, 50);
		add(logo_ATM);
	}
	
	/**
	 * @param e
	 */
	public void addActionListener(final ActionListener e){
		btn_Back.addActionListener(e);
		btn_Deposit.addActionListener(e);
	}
	
	/**
	 * Clears the text field and sets the deposit amount to zero at startup
	 */
	public void clearField() {
		textField_Deposit.setText("");
		depositAmount = 0;
	}
	
	/** Shows the total balance on the selected account
	 * @param totalBalance
	 */
	public void showField(final double totalBalance){
		textField_CurrentBalance.setText("" + totalBalance);
	}
	
	/**
	 * @return the back button
	 */
	public JButton getBack(){
		return btn_Back;
	}
	
	/**
	 * @return the Deposit button
	 */
	public JButton getBtnDeposit(){
		return btn_Deposit;
	}
	
	/** Reads the deposit amount
	 * @return the entered amount in the gui
	 */
	public double getInputDepositAmount() {
		DecimalFormat paymentFormat;
		paymentFormat = (DecimalFormat) NumberFormat.getNumberInstance();
	    paymentFormat.setMaximumFractionDigits(2);
		try {
			depositAmount = paymentFormat.parse(textField_Deposit.getText()).intValue();
		} catch (ParseException exc) {
			exc.getMessage();
		}
		return depositAmount;
	}
	
	/** Adding a mouselistener to the class, by instantiating a MouseAdapter in an anonymous class 
	 * When the mouse is hovering over an object, the tooltip is enabled
	 * When the mouse is not hovering over an object, the tooltop is disabled
	 */
	MouseListener mouseListener = new MouseAdapter(){    
	    final int defaultTimeout = ToolTipManager.sharedInstance().getInitialDelay();
	    @Override
	    public void mouseEntered(final MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(0);
	    }

	    @Override
	    public void mouseExited(final MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
	    }
	};
	
} // end class AccDeposit